package com.zoho.daliansheng.utils;

import com.alibaba.fastjson.JSONObject;
import com.zoho.daliansheng.config.DatasourceEnum;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 通用工具类
 */

@Slf4j
@UtilityClass
public class CommonUtil {

    // 时间保留到时分秒
    public String formatTime(String time) {
        return StringUtils.isNotBlank(time) ? time.substring(0, 10) : "";
    }

    // 格式化小数
    public BigDecimal formatBigDecimal(BigDecimal num, int saveCount) {
        return Objects.isNull(num) ? new BigDecimal("0") : num.setScale(saveCount, RoundingMode.HALF_UP);
    }

    // 是否字段判断
    public boolean formatIsField(Integer isField) {
        if (Objects.nonNull(isField)) {
            return isField == 1;
        }
        return false;
    }

    public String getdataSourceByName(String name){
        if(StringUtils.isBlank(name)){
            return "";
        }
        if(name.equals(DatasourceEnum.GONG_CHANG.getCompanyName())){
            return DatasourceEnum.GONG_CHANG.getAccountCode();
        }else if(name.equals(DatasourceEnum.SU_ZHOU.getCompanyName())){
            return DatasourceEnum.SU_ZHOU.getAccountCode();
        }else if(name.equals(DatasourceEnum.CHANG_ZHOU.getCompanyName())){
            return DatasourceEnum.CHANG_ZHOU.getAccountCode();
        }else if(name.equals(DatasourceEnum.DA_LIAN.getCompanyName())){
            return DatasourceEnum.DA_LIAN.getAccountCode();
        }else if(name.equals(DatasourceEnum.XIN_CAI_LIAO.getCompanyName())){
            return DatasourceEnum.XIN_CAI_LIAO.getAccountCode();
        }
        return "";
    }



    // 根据erp人员编码，从CRM用户中查询用户
    public JSONObject getUser(String personcode, List<JSONObject> allUserData) {
        if(StringUtils.isBlank(personcode)){return null;}
        List<JSONObject> userList = allUserData.stream().filter(object -> Objects.nonNull(object.getString("erp")) && object.getString("erp").equals(personcode)).collect(Collectors.toList());
        if (userList.size() > 0) {
            String userId = userList.get(0).getString("id");
            JSONObject userObj = new JSONObject();
            userObj.put("id", userId);
            return userObj;
        }
        return null;
    }

    // 根据erp业务号，从CRM 销售发货单查数据
    public JSONObject getConsignment(String code,List<JSONObject> moduleData) {
        if(StringUtils.isBlank(code)){return null;}
        List<JSONObject> list = moduleData.stream().filter(object -> object.getString("Name").equals(code)).collect(Collectors.toList());
        if (list.size() > 0) {
            String consignmentId = list.get(0).getString("id");
            JSONObject userObj = new JSONObject();
            userObj.put("id", consignmentId);
            return userObj;
        }
        return null;
    }

    // 根据erp 客户编码，从CRM 客户模块查数据
    public JSONObject getCustomer(String customerCode, List<JSONObject> moduleData ) {
        if(StringUtils.isBlank(customerCode)){return null;}
        List<JSONObject> list = moduleData.stream().filter(object -> Objects.nonNull(object.getString("ERP_account_num")) && object.getString("ERP_account_num").equals(customerCode)).collect(Collectors.toList());
        if (list.size() > 0) {
            String consignmentId = list.get(0).getString("id");
            JSONObject userObj = new JSONObject();
            userObj.put("id", consignmentId);
            return userObj;
        }
        return null;
    }



    // 根据erp 存货编码，从CRM 产品模块查数据
    public JSONObject getProduct(String code, List<JSONObject> moduleData) {
        if(StringUtils.isBlank(code)){return null;}
        List<JSONObject> list = moduleData.stream().filter(object -> Objects.nonNull(object.getString("Product_Code")) && object.getString("Product_Code").equals(code)).collect(Collectors.toList());
        if (list.size() > 0) {
            String consignmentId = list.get(0).getString("id");
            JSONObject userObj = new JSONObject();
            userObj.put("id", consignmentId);
            return userObj;
        }
        return null;
    }
}
